package com.eco.fanliapp.admin.controller.web;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eco.fanliapp.admin.annotation.SysLogAnnotation;
import com.eco.fanliapp.admin.common.BaseController;
import com.eco.fanliapp.common.response.ServerResponse;
import com.eco.fanliapp.entity.Administrator;
import com.eco.fanliapp.entity.AppGoodsClassify;
import com.eco.fanliapp.entity.SysLog;
import com.eco.fanliapp.service.IAppGoodsClassifyService;
import com.eco.fanliapp.service.SysLogCommon;
import com.eco.fanliapp.vo.UserVO;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 商品分类controller
 *
 */
@RestController
@RequestMapping("/web/goodsClassify")
public class AppGoodsClassifyController extends BaseController {

    @Autowired
    private IAppGoodsClassifyService appGoodsClassifyService;
    @Autowired
    private SysLogCommon sysLogCommon;

    /**
     * 添加商品分类
     * @param appGoodsClassify
     * @return
     */
    @RequestMapping(value = "add",method = RequestMethod.POST)
    public ServerResponse appGoodsList(@RequestBody AppGoodsClassify appGoodsClassify) {
        int i = appGoodsClassifyService.insertAppGoodsClassify(appGoodsClassify);
        if (i <= 0) return ServerResponse.createByErrorMessage("商品添加失败");
        SysLog sysLog = new SysLog();
        UserVO authentication = getAuthentication();
        sysLog.setUserId(authentication.getUserId());
        sysLog.setMethodExplain(authentication.getUserId()+"--添加了商品分类--"+appGoodsClassify.getCname());
        sysLog.setResponseParams("成功");
        sysLogCommon.save(sysLog);
        return ServerResponse.createBySuccessMessage("商品添加成功");
    }

    /**
     * 修改商品分类
     * @param appGoodsClassify
     * @return
     */
    @RequestMapping(value = "update",method = RequestMethod.POST)
    public ServerResponse update(@RequestBody AppGoodsClassify appGoodsClassify) {
        int i = appGoodsClassifyService.updateAppGoodsClassify(appGoodsClassify);
        if (i <= 0) return ServerResponse.createByErrorMessage("商品分类更新失败");
        SysLog sysLog = new SysLog();
        UserVO authentication = getAuthentication();
        sysLog.setUserId(authentication.getUserId());
        sysLog.setMethodExplain(authentication.getUserId()+"--修改了商品分类--"+appGoodsClassify.getCname());
        sysLog.setResponseParams("成功");
        sysLogCommon.save(sysLog);
        return ServerResponse.createBySuccessMessage("商品分类更新成功");
    }
    /**
     * 删除商品分类 可以多个 以逗号隔开
     * @return
     */
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    @SysLogAnnotation(value = "删除了商品分类",userType = 2)
    public ServerResponse delete(@RequestBody Map ids) {
        int i = appGoodsClassifyService.deleteAppGoodsClassifyByIds(MapUtils.getString(ids,"ids"));
        if (i <= 0) return ServerResponse.createByErrorMessage("商品分类删除失败");
        return ServerResponse.createBySuccessMessage("商品分类删除成功");
    }

    @RequestMapping(value = "list")
    public ServerResponse list(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "20")Integer pageSize) {
        Page<AppGoodsClassify> page = new Page<>(pageNum, pageSize);
        //查询条件
        QueryWrapper<AppGoodsClassify> queryWrapper = new QueryWrapper<>();
        IPage<AppGoodsClassify> AppGoodsClassifyList = appGoodsClassifyService.page(page, queryWrapper);
        return ServerResponse.createBySuccess("数据获取成功", AppGoodsClassifyList);
    }


}
